
import NFA.Test2;
import org.hcm.compile.main.ocmpile;
import tree.Main;

import javax.swing.*;
import java.awt.*;
import java.io.*;

public class MainFrame extends JFrame {

    private static final long serialVersionUID = -6585678562187194388L;
    private JTextArea jta_editor=new JTextArea();
    private JTextArea jta_result=new JTextArea();

    private JLabel jl_sourse=new JLabel("代码");
    private JLabel jl_result=new JLabel("结果");
    private JScrollPane jsp_editor=new JScrollPane(jta_editor);
    private JScrollPane jsp_result=new JScrollPane(jta_result);
    private JPanel jp_north=new JPanel(new GridLayout(1,2,20,0));
    private JPanel jp_center=new JPanel(new GridLayout(1,2,20,10));
    private JPanel jp_south=new JPanel(new FlowLayout(FlowLayout.CENTER,20,20));
    private JButton btn_analyze=new JButton("词法分析");
    private JButton btn_read=new JButton("读取文件");
    private JButton btn_clean =new JButton("清空");
    private JButton btn_LL_and_four =new JButton("语法分析");
    private JButton btn_NFADFA=new JButton("NFA-DFA-最简");
    private JButton btn_suanfu=new JButton("算符优先分析法");
    private JButton btn_tree=new JButton("语法树");
    private JButton btn_LR=new JButton("LR");
    String path="D:\\java_project\\u\\src\\file\\test00.c";
    public MainFrame() {
        setTitle("编译器");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(new Dimension(1000,800));
        setMinimumSize(new Dimension(500,400));
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());
        jta_editor.setFont(new Font("宋体",Font.BOLD,20));
        jta_result.setFont(new Font("宋体",Font.BOLD,16));
        jta_result.setEditable(false);
//        jsp_editor.setRowHeaderView(new LineNumberHeaderView());
        jsp_editor.setBorder(BorderFactory.createEtchedBorder());
        jp_north.setBorder(BorderFactory.createEmptyBorder(20,20,0,20));
        jp_north.add(jl_sourse);
        jp_north.add(jl_result);
        jp_south.add(btn_clean);
        jp_south.add(btn_read);
        jp_south.add(btn_analyze);
        jp_south.add(btn_LL_and_four);
        jp_south.add(btn_LR);
        jp_south.add(btn_NFADFA);
        jp_south.add(btn_suanfu);


        jp_center.setBorder(BorderFactory.createEmptyBorder(20,20,0,20));
        jp_center.add(jsp_editor);
        jp_center.add(jsp_result);
        jp_center.add(jsp_result);
        btn_read.addActionListener((e)->{
            JFileChooser jfc=new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
            jfc.showDialog(new JLabel(), "选择");
            File file=jfc.getSelectedFile();
            path=file.getAbsolutePath();
            if(file.isDirectory()){
                System.out.println("文件夹:"+file.getAbsolutePath());
            }else if(file.isFile()){
                String textLine;
                System.out.println("文件:"+file.getAbsolutePath());
                FileReader fr = null;
                try {
                    fr = new FileReader(file.getAbsolutePath());
                    BufferedReader reader = new BufferedReader(fr) ;
                    jta_editor.read(reader, "jta_editor");
                    while((textLine=reader.readLine()) != null) {
                        jta_editor.read(reader, "jta_editor");
                    }
                } catch (FileNotFoundException fileNotFoundException) {
                    fileNotFoundException.printStackTrace();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }

            }
            System.out.println(jfc.getSelectedFile().getName());

        });
        btn_LL_and_four.addActionListener(
                (e ->{
                    jta_result.setText("");
                    a a = new a();
                    try {
                        a.mainMethod(jta_editor.getText(),jta_result);
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                })
        );
        btn_NFADFA.addActionListener((e)->{
            Test2 test2=new Test2();
        });
        btn_suanfu.addActionListener((e)->{
            new Jsuanfu();
        });
        btn_LR.addActionListener((e)->{
            new LR();
        });
        //添加按钮事件，执行词法分析
        btn_analyze.addActionListener((e)->{
            jta_result.setText("");
            jta_result.setText("");
            ocmpile ocmpile=new ocmpile();
            try {
                ocmpile.lexAnalyse(path,jta_result);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        });
        btn_clean.addActionListener((e)->{
            jta_result.setText("");
            ocmpile ocmpile=new ocmpile();
            try {
                ocmpile.parser(path,jta_result);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        });
        add(jp_north,BorderLayout.NORTH);
        add(jp_center,BorderLayout.CENTER);
        add(jp_south,BorderLayout.SOUTH);

        setVisible(true);
    }

    public static void main(String[] args) {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    System.out.println(info.getClassName());
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
            JFrame.setDefaultLookAndFeelDecorated(true);
                UIManager.setLookAndFeel("com.jtattoo.plaf.graphite.GraphiteLookAndFeel");
            new MainFrame();
        }catch(Exception e) {
            System.out.println(e);
        }



    }
}
